<?php

class Core_Frontend_Component_Search_Controller_Result extends Dcmp_Component_Controller {

    public function indexAction($tag) {
        $helper = App::getHelper('tag')->registerKeyword(trim($tag));
        $stat = $helper->statRecord($tag)->toArray();
        if(count($stat)){
        $type = $stat[0]['type'];
//        if((mb_strlen($tag)) % 2 != 0){
//            $tag .= ' ';
//        }
        $pageTemplate = App::getModel('design/collection_page_template')
                            ->filterByPageThemeId('493eca751f6fceb07968771690e59698')
                            ->filterByFileName('conlist')
                            ->filterByEnabled(1)
                            ->load()
                            ->getFirstItem();
        $this->getVariables()->setData('page_template', $pageTemplate);
        return $this->redirectResult($this->getUrl(array('tag'=>urlencode($tag), 'type'=>$type), 'list'));
        }
        return $this->viewResult(NULL,'noresult');
    }

    public function listAction($tag, $type = NULL, $page = 1) {
        $tag = trim($tag);
        $helper = App::getHelper('tag');
        $stat = $helper->statRecord($tag)->toArray();
        $ids = $helper->searchRecordIds($tag, $type)->getFieldValues('record_id');
        $pageTemplate = App::getModel('design/collection_page_template')
                            ->filterByPageThemeId('61c35635157c11e08977dc18e12206b6')
                            ->filterByFileName('conlist')
                            ->filterByEnabled(1)
                            ->load()
                            ->getFirstItem();
        $this->getVariables()->setData('page_template', $pageTemplate);
        if(empty($ids)){
            return $this->viewResult(array(), 'noresult');
        }
//        if((mb_strlen($tag)) % 2 != 0){
//            $tag .= ' ';
//        }
        $list = null;
        switch ($type) {
            case 'article':
                $list = App::getModel('article/collection_content');
                break;
            case 'concessions':
                $list = App::getModel('promotion/collection_concessions');
                break;
            case 'subject':
                $list = App::getModel('subject/collection_content');
                break;
        }
        $list->filterByIds($ids)->filterByStatus('published')->pagination($page, 20)->load();
        $records = $list->loadCount();
        return $this->viewResult(array('stat' => $stat, 'tag' => $tag, 'list'=>$list, 'records'=>$records, 'type'=>$type));
    }
//
//    public function articleAction($tag, $page = 1) {
//        $helper = App::getHelper('tag')->registerKeyword($tag);
//        $stat = $helper->statRecord($tag)->toArray();
//        $firstType = $stat[0]['type'];
//        $ids = $helper->searchRecordIds($tag, $firstType)->getFieldValues('record_id');
//        $list = App::getModel('article/collection_content');
//        $list->filterByIds($ids)->pagination($page, 20)->load();
//        $records = $list->loadCount();
//        return $this->viewResult(array('stat' => $stat, 'tag' => $tag, 'list'=>$list, 'records'=>$records), $firstType);
//    }
}

